/**
 * created by 小卷毛, 2020/12/18
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.sgcc.pda.mdrh.task.safe.service;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;

/**
 * 描 述：Android5.0以上网络监听管理类
 * 作 者：小卷毛  2020-12-18 17:06
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
public class NetworkManager {

    private static String TAG = NetworkManager.class.getSimpleName();

    private ConnectivityManager mManager;
    private ConnectivityManager.NetworkCallback mCallback;

    // 私有化构造函数
    private NetworkManager() { }

    private static class NetworkManagerHolder {
        static final NetworkManager INSTANCE = new NetworkManager();
    }

    /**
     * 获取单例对象
     */
    public static NetworkManager getInstance() {
        return NetworkManagerHolder.INSTANCE;
    }

    /**
     * 监听网络连接状态
     */
    public void registerNetwork(
            Context context,
            final INetworkListener listener
    ) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (mManager == null) {
                mManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            } else {
                // 解除注册网络监听
                if (mCallback != null)
                    mManager.unregisterNetworkCallback(mCallback);
            }

            if (mManager != null) {
                if (mCallback == null) {
                    mCallback = new ConnectivityManager.NetworkCallback() {
                        @Override
                        public void onAvailable(@NonNull Network network) {
                            super.onAvailable(network);
                            Log.i(TAG, "网络连接成功！");

                            if (listener != null) listener.connnect();
                        }

                        @Override
                        public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) {
                            super.onBlockedStatusChanged(network, blocked);
                            Log.i(TAG, "网络被阻止或解除阻塞！");
                        }

                        @Override
                        public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
                            super.onCapabilitiesChanged(network, networkCapabilities);
                            Log.i(TAG, "网络状态改变但仍旧是可用！");
                        }

                        @Override
                        public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties linkProperties) {
                            super.onLinkPropertiesChanged(network, linkProperties);
                            Log.i(TAG, "网络连接的属性改变！");
                        }

                        @Override
                        public void onLosing(@NonNull Network network, int maxMsToLive) {
                            super.onLosing(network, maxMsToLive);
                            Log.i(TAG, "网络正在断开连接！");
                        }

                        @Override
                        public void onLost(@NonNull Network network) {
                            super.onLost(network);
                            Log.i(TAG, "网络已断开！");

                            if (listener != null) listener.disconnect();
                        }

                        @Override
                        public void onUnavailable() {
                            super.onUnavailable();
                            Log.i(TAG, "网络连接超时或网络请求达不到可用要求！");
                        }
                    };
                }

                mManager.registerNetworkCallback(
                        new NetworkRequest.Builder().build(),
                        mCallback);
            }
        }
    }

    /**
     * 网络连接状态接口
     */
    interface INetworkListener {
        void connnect();
        void disconnect();
    }

}
